<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 *
 *
 * @package     h
 * @subpackage  models
 * @category    models
 * @author      hyw
 * @copyright   copyright (c) 2012
 * @filesource
 */
class Circle_model extends Base_model {

    protected $_table_name = 'circle';
    protected $_key_name = 'circle_id';

    function get_info($id) {
        if (!empty($id))
            $where[$this->_key_name] = $id;
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, 'row');
    }

    function insert_sql_where($text) {

        $data = array(
            'SQL' => $text
        );

        if (!empty($text))
            $this->db->insert('h_sql', $data);
    }

    function get_area_circle($data) {
        $where["city"] = $data['city'];
        $where["area"] = $data['area'];
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where);
    }

    function get_all() {
        $this->set_order_by('sequence,ASC');
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }

    function get_all_count() {
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where, NULL, "count");
    }

    function get_not_in($i) {
        $v = explode(",", $i);
        $this->db->where_not_in('value', array_map('trim', $v));
        $this->set_order_by('sequence,ASC');
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }

    function get_circle() {
        $this->set_order_by('sequence,ASC');
        $where = array('dtime' => 0);
        $result = $this->fetch(NULL, $where);
        $p = array();
        foreach ($result as $key => $val) {
            $p[$val->circle_id] = $val->value;
        }
        return $p;
    }

    function query_keyword($keyword = "", $limit) {
        if ($keyword != "")
            $this->set_or_like('value', $keyword);
        $this->set_limit($limit);
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }

    function query_keyword_count($keyword = null) {
        if ($keyword != "")
            $this->set_or_like('value', $keyword);
        $con["where"][] = array('dtime' => 0);
        $this->set_args_array($con);
        return $this->fetch_count();
    }

    function get_demand_circle($keyword) {
        if ($keyword != "")
            $this->db->like('value', $keyword, 'after');
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $where['dtime'] = 0;
        $result = $this->fetch(NULL, $where);
        $p = array();
        foreach ($result as $key => $val) {
            $p[$val->circle_id]["value"] = urlencode($val->value);
            $p[$val->circle_id]["explanation"] = urlencode($val->explanation);
        }
        return $p;
    }

    function update_data($data) {
        $value = array('city' => $data['city'], 'area' => $data['area'], 'value' => $data['value'], 'sequence' => $data['sequence'], 'mtime' => time());
        $where[] = array($this->_key_name => $data['id']);
        return $this->update($value, $where);
    }

    function add($data) {
        $data['ctime'] = time();
        return $this->insert($data);
    }

    function del($data) {
        $where[] = array($this->_key_name => $data['id']);
        $value = array('dtime' => time());
        return $this->update($value, $where);
    }

    function get_c_all() {
        $this->set_limit("20", "0");
        $res = $this->db->get($this->_table_name);
        return $res;
    }

    function get_fields() {
        $fields = $this->db->list_fields($this->_table_name);

        return $fields;
    }

    function get_country_circle($city, $area) {
        //$where['city']=$data['city'];
        //$where['area']=$data['area'];
        //$this->db->like('city', $data['city'], 'none'); 
        //$this->db->like('area', $data['area'], 'none'); 

//        $data=array("SQL"=>$city.'/'.$area);    
//        $this->db->insert("h_sql",$data);
        
        $sql_c = "SELECT * FROM  `h_circle`  WHERE " .
                " `city` like  '" . $city . "'".
                " AND `area` like  '" . $area . "'";
        
        //////////////////////////////////
//        $data=array("SQL"=>$sql_c);    
//        $this->db->insert("h_sql",$data);
        //////////////////////////////////
        $result_c = $this->db->query($sql_c);

//        $cc;
//        foreach ($result_c->result() as $val) {
//            $cc = $val->name;
//        }
//
//        $sql = '';
//        if ($area == 128) {
//            $sql = "SELECT * FROM  `h_circle`  WHERE  `city` like  '%台北市%'  AND  `area` like  '%中正區%'";
//        } else {
//            $sql = "SELECT * FROM  `h_circle`  WHERE " .
//                    " `city` like  '%" . $city . "%' " .
//                    " AND  `area` like  '%" . trim($cc) . "%'";
//        }
//        
//        $result = $this->db->query($sql);
        $p = array();
        foreach ($result_c->result() as $val) {
            $p[$val->circle_id] = $val->value;
        }
        return $p;
    }


    function get_country_circle_victor( $area) {

        
        $sql_c = "SELECT * FROM  `h_circle`  WHERE " .
                " `sequence` =  '" . $area . "'";
                
        
        
        
        $result_c = $this->db->query($sql_c);


        $p = array();
        foreach ($result_c->result() as $val) {
            $p[$val->circle_id] = $val->value;
        }
        return $p;
    }

    function get_country_circle_victor2( $area , $city) {

        
        $sql_c = "SELECT * FROM  `h_circle`  WHERE " .
                " `area` like  '%" . $area . "%'".
                " AND ".
                " `city` like  '%" . $city . "%'";
                
        
        
        
        $result_c = $this->db->query($sql_c);


        $p = array();
        foreach ($result_c->result() as $val) {
            $p[$val->circle_id] = $val->value;
        }
        return $p;
    }

}
